InputDataHandler{

	classvar <>namesInstantiated;
	var scalerPlugin, smoothArray, howSmooth = 100, gui, recieveFunction, name, intensityMeter;
	
	*new{
		^super.new.init;
	}
	
	setName{|nameArg|
		name = nameArg;
		namesInstantiated.add(nameArg);
	}
	
	getName{
		^name;
	}
	
	init{
		scalerPlugin = ScalerPlugin.new;
		smoothArray = List.new;
	}
	
	launchGui{
		gui = InputDataHandlerGui.new(this);
	}
	
	setInputLow{|low = 0|
		scalerPlugin.setValue(\input1, low);
	}
	
	setInputHigh{|high = 1|
		scalerPlugin.setValue(\input2, high);
	}
	
	getInputHigh{
		^scalerPlugin.getVals.at(\input2);	
	}
	
	
	setOutputLow{|low = 0|
		scalerPlugin.setValue(\output1, low);
	}
	
	setOutputHigh{|high = 1|
		scalerPlugin.setValue(\output2, high);
	}
	
	
	prSmooth{|val|
		smoothArray.addFirst(val);
		smoothArray = smoothArray.keep(howSmooth);
		^smoothArray.mean	
	}
	
	setHowSmooth{|val|
		howSmooth = val;
	}
	
	process{|val|
		val = this.prSmooth(val);
		val = scalerPlugin.process(val); 
		{
			intensityMeter.background_(Color.new(0,val,0));
			intensityMeter.refresh;
		}.defer;
		
		^val
	}
	
	recieveInput{|input|
		recieveFunction.value(this.process(input));
	}
	
	setRecieveFunction{|fn|
		recieveFunction = fn;	
	}
	
	createGui{|parent|
		var window, handler, text;
		window = FlowView.new(parent, Rect.new(0, 0, 500, 50));		handler = this;
		
		
		text = SCStaticText(window, Rect(10, 10, 400, 20));
		text.string = name;
		text.font = (Font("Helvetica-Bold", 20));
		
		window.startRow;
		
		EZSlider(window, // window
				400 @ 24, // dimensions
				"Dampening", // label
				ControlSpec(0.001, 10, \exp), // control spec
				{|ez| handler.setInputHigh(ez.value) },// action
				handler.getInputHigh // initVal
				);
		intensityMeter = FlowView.new(window, Rect.new(0, 0, 24, 24));
		intensityMeter.background_(Color.black);
		
	}

}

PeakInputDataHandler : InputDataHandler{
	
	var howResponsive = 1;
	
	setHowResponsive{|val|
		howResponsive = val;
	}
	
	prSmooth{|val|
		var numToReplace;
		
		if(smoothArray.at(0) == nil){
			howSmooth.do{
				smoothArray.add(0);
			}
		};
		
		if(val < smoothArray.at(0)){
			smoothArray.fill(smoothArray.mean  - (smoothArray.mean/howSmooth));
			^smoothArray.mean;
			//^super.prSmooth( smoothArray.mean  - (smoothArray.mean/100) );
		};
		
		numToReplace = howSmooth.div(1/howResponsive);
		smoothArray = smoothArray.putSeries(0, 1, numToReplace, val);
		^smoothArray.mean;
	}
	
}












